import 'dart:convert' show json;
import 'package:mahjong/entity/UserEntityInfo.dart';
import 'package:mahjong/entity/meta/MetaUserChannel.dart';
import 'package:mahjong/entity/card/MahjongUserSwapCard.dart';

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }
  return null;
}

class MahjongUserInfo extends MetaUserChannel {
  int integral;
  bool prepare;
  int gameType;
  int huCard;
  bool swap;
  bool listening;
  int winCount;
  String userId;
  List<int> callHuCards;
  String firingUser;

  // 交换信息
  MahjongUserSwapCard userSwapCard;

  void updateUser(MahjongUserInfo user) {
    this.userId = user.userId;
    this.banker = user.banker;
    this.seq = user.seq;
    this.online = user.online;
    this.win = user.win;
    this.integral = user.integral;
    this.prepare = user.prepare;
    this.gameType = user.gameType;
    this.huCard = user.huCard;
    this.swap = user.swap;
    this.sysUser = user.sysUser;
    this.listening = user.listening;
    this.winCount = user.winCount;
    this.callHuCards = user.callHuCards;
    this.userSwapCard = user.userSwapCard;
    this.firingUser = user.firingUser;
  }

  MahjongUserInfo(
      {this.integral,
      this.prepare,
      this.gameType,
      this.huCard,
      this.swap,
      this.userId,
      this.listening,
      this.winCount,
      this.userSwapCard,
      this.firingUser});

  MahjongUserInfo.init(Map<String, dynamic> jsonRes) {
    final List<int> callHuCards =
        jsonRes['callHuCards'] is List ? <int>[] : null;
    if (callHuCards != null) {
      for (final dynamic item in jsonRes['callHuCards']) {
        if (item != null) {
          callHuCards.add(asT<int>(item));
        }
      }
    }

    final List<int> callExportCards =
        jsonRes['callExportCards'] is List ? <int>[] : null;
    if (callExportCards != null) {
      for (final dynamic item in jsonRes['callExportCards']) {
        if (item != null) {
          callExportCards.add(asT<int>(item));
        }
      }
    }

    this.integral = asT<int>(jsonRes['integral']);
    this.prepare = asT<bool>(jsonRes['prepare']);
    this.gameType = asT<int>(jsonRes['gameType']);
    this.huCard = asT<int>(jsonRes['huCard']);
    this.winCount = asT<int>(jsonRes['winCount']);
    this.swap = asT<bool>(jsonRes['swap']);
    this.userId = asT<String>(jsonRes['userId']);
    this.banker = asT<bool>(jsonRes['banker']);
    this.listening = asT<bool>(jsonRes['listening']);
    this.seq = asT<int>(jsonRes['seq']);
    this.win = asT<bool>(jsonRes['win']);
    this.online = asT<bool>(jsonRes['online']);
    this.sysUser = SysUser.fromJson((jsonRes)['sysUser']);
    this.userSwapCard = MahjongUserSwapCard.fromJson((jsonRes)['userSwapCard']);
    this.callHuCards = callHuCards;
    this.firingUser = asT<String>(jsonRes["firingUser"]);
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'userId': userId,
        'banker': banker,
        'seq': seq,
        'win': win,
        'online': online,
        'integral': integral,
        'prepare': prepare,
        'gameType': gameType,
        'huCard': huCard,
        'swap': swap,
        'listening': listening,
        'sysUser': sysUser,
        'winCount': winCount,
        'callHuCards': callHuCards,
        'userSwapCard': userSwapCard,
        'firingUser': firingUser
      };
  factory MahjongUserInfo.fromJson(Map<String, dynamic> jsonRes) =>
      jsonRes == null ? null : MahjongUserInfo.init(jsonRes);

  @override
  String toString() {
    return json.encode(this);
  }
}
